package com.shujia.scala

import scala.io.Source

object Demo14ClassNum {
  def main(args: Array[String]): Unit = {
    //1、读取文件
    val students: List[String] = Source.fromFile("data/students.txt").getLines().toList

    //2、解析数据取出班级
    //last:获取最后一个元素
    val classs: List[String] = students
      .map(student => student.split(",").last)

    //3、按照班级进行分组
    val groupByList: Map[String, List[String]] = classs
      .groupBy(clazz => clazz)

    //4、统计班级的人数
    val clazzNum: Map[String, Int] = groupByList
      .mapValues(value => value.length)

    clazzNum.foreach(println)
  }
}
